<?php

namespace App\Console\Commands;

use App\CrmConfig;
use App\Customer;
use App\CustomerRemind;
use App\Notifications\CustomerRemindNotifications;
use Carbon\Carbon;
use http\Client\Curl\User;
use Illuminate\Console\Command;
use PhpOption\LazyOption;

class SeaCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sea:overdue';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '公海到期提醒,每分钟提醒一次';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
           $remindConfig=CrmConfig::whereName('remind_config')->first();
           if ($remindConfig->value==1){
               $followDay= CrmConfig::whereName('follow_day')->first()->value;
               $remindDay= CrmConfig::whereName('remind_day')->first()->value;
               $customer= Customer::whereDoesntHave('remindUser',function($query)use($remindDay){
                   $query->where('5kcrm_crm_customer_remind.created_at','>',Carbon::now()->addDays(-$remindDay));
               })->where('owner_user_id','>',0)->where(
                    'update_time','<',time()-$followDay*86400+$remindDay*86400
               )->where('update_time','>',time()-$followDay*86400)->chunkById(100,function($customers){
                   foreach ($customers as $customer){
                       $this->info($customer->customer_id);
                       //提醒
                      $user=\App\User::find($customer->owner_user_id);
                      if ($user){
                          $user->notify(new CustomerRemindNotifications($customer));
                      }
                      $remind=new CustomerRemind();
                      $remind->customer_id=$customer->customer_id;
                      $remind->owner_user_id=$customer->owner_user_id;
                      $remind->save();
                   }
               });
           }


    }
}
